//
//  PayViewController.swift
//  LiuheCommunity
//
//  Created by 罗恒 on 2018/1/25.
//  Copyright © 2018年 luo. All rights reserved.
//

import UIKit

class PayViewController: UIViewController {

    
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var PhoneNum: UILabel!
    var acId:String?

    @IBAction func btn(_ sender: Any) {
      let model = ShareInfo.shareInstance.modAc
      WebService.shareInstance.wechatpay(activityId: self.acId, price: model?.discountedPrice, timeStamp: "a", nonceStr: "return32LetterAndNumber") {(isSuccess,par)in
        if isSuccess == true {
                let model = ShareInfo.shareInstance.pay
                let req = PayReq.init()
                req.partnerId = "1413386102"
                req.prepayId = model?.prepay_id
                req.package = "Sign=WXPay"
                req.sign = model?.sign
            
            
                let timeInt=NSDate().timeIntervalSince1970
                let a = UInt32(timeInt)
                req.timeStamp = a
                WXApi.send(req)
            
        }else {
            
        }
      }
        
//                let req = PayReq.init()
//                req.partnerId = "1413386102"
//                req.prepayId = "wx2017101017240214acd35dd40213792169"
//                req.package = "Sign=WXPay"
//                req.nonceStr = "return32LetterAndNumber"
//                req.sign = "30909AE46BA26A0CD61D6CC14B048188"
      
    }
    @IBOutlet weak var moneyT: UILabel!
    @IBOutlet weak var classImage: UIImageView!
    @IBOutlet weak var classTitle: UILabel!
    @IBOutlet weak var classDate: UILabel!
    @IBOutlet weak var money: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
    }
    override func viewWillAppear(_ animated: Bool) {
        let model = ShareInfo.shareInstance.modAc
        self.classTitle.text = model?.title
        self.classDate.text = model?.date
        self.money.text = model?.discountedPrice
        self.moneyT.text = model?.discountedPrice
        let picURL = NSURL.init(string: (model?.imgMainUrl)!)
        classImage.sd_setImage(with: picURL! as URL)
        
        WebService.shareInstance.UserInfo{(isSuccess,par)in
            if isSuccess == true{
                let model = ShareInfo.shareInstance.infoAc
                self.name.text = model?.name
                self.PhoneNum.text = model?.phoneNo
            }else{
                let a = ""
                
                MBProgressHUD.showToast(to: self.view, withText: a)
                
            }
            
        }
    }
    
    
    func return32LetterAndNumber(length:Int) -> String {
        let letters:NSString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        let len = UInt32(letters.length)
        var return32LetterAndNumber = ""
        for _ in 0..<length{
            let rand = arc4random_uniform(len)
            var nextChar = letters.character(at: Int(rand))
            return32LetterAndNumber += NSString(characters: &nextChar, length: 1) as String
        }
        return return32LetterAndNumber
    }
    func timeStamp() -> String{
        let now = Date()
        let dformatter = DateFormatter()
        dformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let timeInterval:TimeInterval = now.timeIntervalSince1970
        let timeStamp = String(timeInterval)
        return timeStamp
    }
}


